<script setup>
import { RouterLink, RouterView, useRouter } from 'vue-router'
import { ref, onMounted } from 'vue';
import ExComponent from './components/exComponent.vue';
import LoginModal from './components/LoginModal.vue';
import { useUserStore } from './stores/user.js';

const router = useRouter();
const userStore = useUserStore();

// 登录模态框显示状态
const showLoginModal = ref(false);
// 用户面板显示状态
const showUserPanel = ref(false);

// 初始化用户状态
onMounted(() => {
  userStore.initUserState();
});

// 打开登录模态框
function openLoginModal() {
  showLoginModal.value = true;
}

// 关闭登录模态框
function closeLoginModal() {
  showLoginModal.value = false;
}

// 处理登录成功
async function handleLoginSuccess(info) {
  // 登录成功后，用户信息已经在LoginModal中通过fetchUserInfo获取了
  // 这里只需要关闭模态框
  showLoginModal.value = false;
}

// 切换用户面板显示
function toggleUserPanel() {
  showUserPanel.value = !showUserPanel.value;
}

// 退出登录
function logout() {
  userStore.logout();
  showUserPanel.value = false;
  // 退出登录后返回首页
  router.push('/');
}
</script>

<template>
  <header>
    <div class="header-container">
     
  
    </div>
  </header>
  
  <!-- 登录模态框 -->
  <LoginModal 
    :show="showLoginModal" 
    @close="closeLoginModal"
    @login-success="handleLoginSuccess"
  />
  
  <RouterView 
    @open-login="openLoginModal" 
    @toggle-user-panel="toggleUserPanel"
    @logout="logout"
    :isLoggedIn="userStore.isLoggedIn"
    :userInfo="userStore.userInfo"
    :showUserPanel="showUserPanel"
  />
</template>

<style scoped>


header {
  line-height: 1.5;
  max-height: 100vh;
}

.header-container {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  padding: 0 20px;
}

.logo {
  display: block;
  margin: 0 auto 2rem;
}

nav {
  width: 100%;
  font-size: 12px;
  text-align: center;
  margin-top: 2rem;
}

nav a.router-link-exact-active {
  color: var(--color-text);
}

nav a.router-link-exact-active:hover {
  background-color: transparent;
}

nav a {
  display: inline-block;
  padding: 0 1rem;
  border-left: 1px solid var(--color-border);
}

nav a:first-of-type {
  border: 0;
}

/* 用户区域样式 */
.user-area {
  display: flex;
  align-items: center;
  margin-left: 20px;
}

.login-button {
  padding: 8px 16px;
  background-color: #1e80ff;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
}

.login-button:hover {
  background-color: #0060e0;
}

.user-avatar-container {
  position: relative;
  cursor: pointer;
}

.user-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background-size: cover;
  background-position: center;
  border: 2px solid #eee;
}

/* 用户面板样式 */
.user-panel {
  position: absolute;
  top: 50px;
  right: 0;
  width: 300px;
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.15);
  z-index: 1000;
  overflow: hidden;
}

.user-panel-header {
  display: flex;
  padding: 16px;
  background-color: #f7f8fa;
  border-bottom: 1px solid #e4e6eb;
}

.user-panel-avatar {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  background-size: cover;
  background-position: center;
  margin-right: 12px;
}

.user-panel-info {
  flex: 1;
}

.user-panel-name {
  font-weight: bold;
  margin-bottom: 4px;
}

.user-panel-level {
  font-size: 12px;
  color: #86909c;
  display: flex;
  margin-bottom: 4px;
}

.level-progress {
  height: 4px;
  background-color: #e4e6eb;
  border-radius: 2px;
  overflow: hidden;
}

.level-progress-bar {
  height: 100%;
  width: 50%;
  background-color: #1e80ff;
}

.user-panel-stats {
  display: flex;
  padding: 12px 0;
  border-bottom: 1px solid #e4e6eb;
}

.stat-item {
  flex: 1;
  text-align: center;
}

.stat-value {
  font-weight: bold;
}

.stat-label {
  font-size: 12px;
  color: #86909c;
}

.user-panel-menu {
  padding: 8px 0;
}

.menu-item {
  display: flex;
  align-items: center;
  padding: 10px 16px;
  cursor: pointer;
  font-size: 14px;
}

.menu-item:hover {
  background-color: #f7f8fa;
}

.menu-icon {
  margin-right: 8px;
  font-size: 16px;
  width: 20px;
  text-align: center;
}

.user-panel-footer {
  display: flex;
  justify-content: space-between;
  padding: 12px 16px;
  border-top: 1px solid #e4e6eb;
  font-size: 14px;
  color: #86909c;
}

.settings-link, .logout-link {
  cursor: pointer;
}

.settings-link:hover, .logout-link:hover {
  color: #1e80ff;
}

@media (min-width: 1024px) {
  header {
    display: flex;
    place-items: center;
    padding-right: calc(var(--section-gap) / 2);
  }

  .logo {
    margin: 0 2rem 0 0;
  }

  header .wrapper {
    display: flex;
    place-items: flex-start;
    flex-wrap: wrap;
  }

  nav {
    text-align: left;
    margin-left: -1rem;
    font-size: 1rem;
    padding: 1rem 0;
    margin-top: 1rem;
  }
}
</style>
